package com.friends.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.friends.library.persistence.HibernateUtil;

public class Return extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     * @author HAVE A NICE DAY
     */
    public Return() {
        super();
        // TODO Auto-generated constructor stub
    }

		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		// TODO Auto-generated method stub
			String page="deleteupdate.jsp";
			int bookid=Integer.parseInt(request.getParameter("bookid"));
			String date=request.getParameter("dateissued");
			String bookname=request.getParameter("bookname");
			PrintWriter out=response.getWriter();
			Session session=HibernateUtil.getSessionFactory().openSession();
			Transaction transaction=session.beginTransaction();
			String sql="delete from IssueBook where bookid=:bookid";
			Query query=session.createQuery(sql);
			query.setInteger("bookid",bookid);
			int result=query.executeUpdate();
			/*if(result==0){
				out.println("Book is not returned in to Database");
			}
			else{
				out.println("Book Returned Successfully");
			}*/
			request.setAttribute("bookid",bookid);
			request.setAttribute("date", date);

			RequestDispatcher dispatcher = request.getRequestDispatcher(page);

			if (dispatcher != null){

			dispatcher.forward(request, response);

			} 
					transaction.commit();
				
						session.close();
						
	}

}
